package com.glory.brepda.adapter;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.ScrapEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * FileName: DefectListAdapter
 * Author: XJH
 * Date: 2020/5/28 9:03
 * Description:不良代码集合
 * History:
 */
public class DefectListAdapter extends BaseQuickAdapter<ScrapEntity, BaseViewHolder> {

    private TextView tvTotal;
    private long totalNum = 0;
    private boolean isDSerror;

    public DefectListAdapter(@Nullable List<ScrapEntity> data, TextView textView, boolean isDSerror) {
        super(R.layout.item_defect_list, data);
        this.tvTotal = textView;
        this.isDSerror = isDSerror;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(BaseViewHolder helper, ScrapEntity item) {
        helper.setText(R.id.tvDefectCode, item.getACTIONCODE());
        helper.setText(R.id.tvDefectDesc, item.getACTIONREASON());
        EditText etDefectNum = helper.getView(R.id.etDefectNum);
        ImageView ivDelete = helper.getView(R.id.ivDelete);
        etDefectNum.setText(item.getMAINQTY().equals("")?"0":item.getMAINQTY());
//        item.setMAINQTY("0");
//        item.setSUBQTY("0");
        setEditListener(etDefectNum, item);

        tvTotal.setText(Calculation()<0?"0":Calculation()+"");

        ivDelete.setOnClickListener(view -> {
            remove(helper.getAdapterPosition());
            notifyDataSetChanged();
            long outputNum = Calculation()<0?0:Calculation();
            tvTotal.setText(outputNum+"");

        });

        etDefectNum.setOnFocusChangeListener((view, hasFocus) -> {

            try {
                if (etDefectNum.getText().toString().equals("")) {
                    etDefectNum.setText(0 + "");
                }
                long outputNum = Calculation();
                FreeApi_LogUtils.saveErrorMessages(outputNum+""+item.getACTIONCODE()+"-----"+item.getMAINQTY()+"----"+totalNum,"defectListAdapter");
                if (outputNum < 0) {
                    tvTotal.setText("0");
                    FreeApi_ToastUtils.showFreeToast("不良数量大于批次数量，请重新输入", MyApp.app, false, Toast.LENGTH_LONG);
                    etDefectNum.setText(0 + "");
                } else {
                    tvTotal.setText(outputNum + "");
                }

            } catch (NumberFormatException e) {
                e.printStackTrace();
                FreeApi_ToastUtils.showFreeToast("计算异常，请输入合法数字", MyApp.app, false, Toast.LENGTH_LONG);
            }

        });
    }

    public void setTotalNum(long mainQTY) {
        this.totalNum = mainQTY;
    }

    public long Calculation() {
        long sum = 0;
        List<ScrapEntity> data = getData();

        if (data.size() <= 0) {
            return 0;
        }

        if (isDSerror) {
            for (ScrapEntity entity : data) {
                sum += Integer.parseInt(entity.getMAINQTY().equals("") ? "0" : entity.getMAINQTY());
            }
        } else {
            for (ScrapEntity entity : data) {
                sum += Integer.parseInt(entity.getSUBQTY().equals("") ? "0" : entity.getSUBQTY());
            }
        }

        if (sum > totalNum) {
            sum = -1;
        }

        return sum;
    }

    private void setEditListener(EditText editText, ScrapEntity item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                listener.OnListener(s.toString(),item);
//                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//                    @Override
//                    public void onFocusChange(View view, boolean b) {
//                        if (!b) {
//
//                        }
//                    }
//                });
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    public interface  onListener{
        void OnListener(String msg,ScrapEntity scrapEntity);
    }
    private onListener listener;
    public void setListener( onListener listener){
        this.listener = listener;
    }
}
